<template>
  <div class="container">
    <div class="app-container">
      <div class="left">
        <el-input v-model="keyword" style="margin-bottom:10px" type="text" prefix-icon="el-icon-search" size="small" placeholder="输入员工姓名全员搜索" @input="handleInput" />
        <!-- 树形组件 -->
        <el-tree
          ref="tree"
          node-key="id"
          :data="depts"
          :props="{label:'name' }"
          default-expand-all
          highlight-current
          :expand-on-click-node="false"
          @current-change="handleCurrentChange"
        />
      <!-- expand-on-click-node:点击不收缩
           current-change	当前选中节点变化时触发的事件
           highlight-current点击高亮效果
           node-key配合tree上的setCurrentKey方法，首次进入时高亮
       -->
      </div>

      <div class="right">
        <!-- type=‘flex’开启flex布局说 -->
        <el-row class="opeate-tools" type="flex" justify="end">
          <el-button size="mini" type="primary">添加员工</el-button>

          <el-button size="mini">excel导入</el-button>

          <el-button size="mini">excel导出</el-button>

        </el-row>
        <el-table :data="rows">
          <el-table-column align="center" label="头像" prop="staffPhoto">
            <template #default="{row}">
              <el-avatar v-if="row.staffPhoto" :src="row.staffPhoto" />
              <span v-else class="hhh">{{ row.username.slice(2,3) }}</span>
            </template>
          </el-table-column>
          <el-table-column label="姓名" prop="username" />
          <el-table-column label="手机号" sortable prop="mobile" />
          <el-table-column label="工号" sortable prop="workNumber" />
          <el-table-column label="聘用形式" prop="formOfEmployment">
            <template #default="{row}">
              <span>
                {{ row.formOfEmployment===1?'正式' :'非正式' }}
              </span>
            </template>
          </el-table-column>
          <el-table-column label="部门" prop="departmentName" />
          <el-table-column label="入职时间" sortable prop="timeOfEntry" />
          <el-table-column label="操作" width="280px">
            <template>
              <el-button size="mini" type="text">查看</el-button>
              <el-button size="mini" type="text">角色</el-button>
              <el-button size="mini" type="text">删除</el-button>
            </template>

          </el-table-column>

        </el-table>

        <!-- 分页 -->
        <el-row style="height: 60px" align="middle" type="flex" justify="end">
          <el-pagination
            layout="total,prev, pager, next"
            :total="total"
            :current-page="page"
            @current-change="handlePageChange"
          />
        </el-row>
        <!-- 表格组件 -->
        <!-- 分页 -->
      </div>

    </div>

  </div>

</template>

<script>
import { getDepartments } from '@/api/department'
import { getEmployeeList } from '@/api/employee'
import { transListTreeData } from '@/utils'

export default {
  name: 'Employee',
  data() {
    return {
      keyword: '',
      depts: [],
      selectedId: '',
      total: 0,
      rows: [],
      page: 1// 当前页码
    }
  },
  created() {
    this.getDepts()
  },
  methods: {
    handleInput() {
      this.getEmployeeList()
    },
    handlePageChange(val) {
      // val就是当前点击页码
      this.page = val
      // 重新获取数据
      this.getEmployeeList()
    },
    async getEmployeeList() {
      const { total, rows } = await getEmployeeList({
        page: this.page,
        pagesize: 5,
        departmentId: this.selectedId,
        keyword: this.keyword
      })
      this.total = total
      this.rows = rows
    },
    handleCurrentChange(obj) {
      this.selectedId = obj.id
      // 记录id 将来配合右侧进行数据展示
      // tree组件内部
      // 在点击任意节点时 会触发$emit('current-change')
      // console.log(obj)
      this.page = 1// 每次点击后获取数据的时候要重置页码
      this.getEmployeeList()
    },
    async getDepts() {
      const res = await getDepartments()
      this.depts = transListTreeData(res, 0)
      // 一进入页面 用户不选择部门时 也需要默认获取传智教育的所有员工进行展示
      // 一旦获取数据 立即将传智的id存下来
      this.selectedId = this.depts[0].id
      // 等待tree渲染完成后才能高亮
      this.$nextTick(() => {
        this.$refs.tree.setCurrentKey(this.selectedId)
      })
      this.getEmployeeList()
    }
  }
}
</script>
<style lang="scss" scoped>
.app-container {
  background: #fff;
  display: flex;
  .left {
    width: 280px;
    padding: 20px;
    border-right: 1px solid #eaeef4;
  }
  .right {
    flex: 1;
    padding: 20px;
    .opeate-tools {
      margin:10px ;
    }
    .username {
      height: 30px;
      width: 30px;
      line-height: 30px;
      text-align: center;
      border-radius: 50%;
      color: #fff;
      background: #04C9BE;
      font-size: 12px;
      display:inline-block;
    }
  }
}
.hhh {
  display: inline-block;
  width: 40px;
  height: 40px;
  border-radius: 50%;
  background-color: #8df;

  text-align: center;
  line-height: 40px;
  color: #fff;
  font-size: 20px;
}
</style>
